package com.graduate.util;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL) //null的对象不需要显示
public class ResponseResult<T> implements Serializable {
	private int status; //状态码
	private String msg; //消息
	private T data;     //数据

	//构造器，状态码必须的
	private ResponseResult(int status) {
		this.status=status;
	}
	
	private ResponseResult(int status,T data) {
		this.status=status;
		this.data=data;
	}
	private ResponseResult(int status,String msg,T data) {
		this.status=status;
		this.msg=msg;
		this.data=data;
	}
	private ResponseResult(int status,String msg) {
		this.status=status;
		this.msg=msg;
	}
	
	
	public int getStatus() {
		return status;
	}
	public String getMsg() {
		return msg;
	}
	public T getData() {
		return data;
	}
	@JsonIgnore
	public boolean isSuccess() {
		return this.status==ResponseCode.SUCCESS.getCode();
	}
	
	//成功的情况！！
	/**
	 * 返回成功的状态码：status=0
	 * @return
	 */
	public static <T> ResponseResult<T> createBySuccessCode(){
		return new ResponseResult<>(ResponseCode.SUCCESS.getCode());
	}
	/**
	 * 返回成功的状态码：status=0，
	 * 返回成功的消息： msg
	 * @param msg
	 * @return
	 */
	public static <T> ResponseResult<T> createBySuccessMsg(String msg){
		return new ResponseResult<>(ResponseCode.SUCCESS.getCode(), msg);
	}
	/**
	 * 返回成功的状态码：status=0，
	 * 返回成功的数据： data
	 * @param msg
	 * @return
	 */
	public static <T> ResponseResult<T> createBySuccessData(T data){
		return new ResponseResult<>(ResponseCode.SUCCESS.getCode(),data);
	}
	/**
	 * 返回成功的状态码：status=0
	 * 返回成功的消息：msg
	 * 返回成功的数据：data
	 * @param msg
	 * @param data
	 * @return
	 */
	public static <T> ResponseResult<T> createBySuccess(String msg,T data){
		return new ResponseResult<>(ResponseCode.SUCCESS.getCode(),msg,data);
	}
	
	//失败的情况！！
	/**
	 * 返回失败的状态码：status=1
	 * @return
	 */
	public static <T> ResponseResult<T> createByFailedCode(){
		return new ResponseResult<>(ResponseCode.FAILED.getCode());
	}
	/**
	 * 返回失败的状态码：status=0，
	 * 返回失败的消息： msg
	 * @param msg
	 * @return
	 */
	public static <T> ResponseResult<T> createByFailedMsg(String msg){
		return new ResponseResult<>(ResponseCode.FAILED.getCode(), msg);
	}
	/**
	 * 返回失败的状态码：status=0，
	 * 返回失败的数据： data
	 * @param msg
	 * @return
	 */
	public static <T> ResponseResult<T> createByFailedData(T data){
		return new ResponseResult<>(ResponseCode.FAILED.getCode(),data);
	}
	/**
	 * 返回失败的状态码：status=0
	 * 返回失败的消息：msg
	 * 返回失败的数据：data
	 * @param msg
	 * @param data
	 * @return
	 */
	public static <T> ResponseResult<T> createByFailed(String msg,T data){
		return new ResponseResult<>(ResponseCode.FAILED.getCode(),msg,data);
	}
}
